<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
class Cgraph extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dolist   = C\router::get_url('admin-live-log-graph', 'dolist');
		$view->assign_by_ref('dolist', $dolist);
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/log/graph/graph.ext.js');
	}
	
	public function frame() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$graph = C\router::get_url('admin-live-log-graph', 'framehtml');
		$view->assign_by_ref('graph', $graph);
		$view->display('live/log/graph/frame.ext.js');
	}
	
	public function framehtml() {
		$view  = C\view::inst('html');
		Cadmin::chkaccess($view);
		$url   = C\router::get_url('admin-live-log-graph', 'framejson');
		$view->assign_by_ref('url', $url);
		$view->display('live/log/graph/frame-graph.html');
	}
	
	public function framejson() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$studioid = input::get_post('studioid');
		$eventid  = input::get_post('eventid');
		$sorm   = C\model::load('log.log.eventlog')->get_orm();
		$stable = C\model::load('log.log.eventlog')->get_table();
		$sql    = 'select averfps,averflow,avercamfps,stime from '.$stable.' where studioid='.$studioid . ' and eventid='.$eventid . ' order by id desc limit 120';
		$list   = $sorm->get_list($sql);
		if (!empty($list)) {
			$fps = $flow = $camfps = $time = array();
			$i   = 1;
			foreach ($list as $item) {
				$fps[]    = $item['averfps'];
				$flow[]   = $item['averflow'];
				$camfps[] = $item['avercamfps'];
				$time[]   = $i;
				$i++;
			}
			$res  = array('time'=>$time, 'fps'=>$fps, 'flow'=>$flow, 'camfps'=>$camfps, 'agent'=>$_SERVER['HTTP_USER_AGENT']);
			ob_end_clean();
			echo json_encode($res);
		}
	}
	
	public function srs() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$graph = C\router::get_url('admin-live-log-graph', 'srshtml');
		$view->assign_by_ref('graph', $graph);
		$view->display('live/log/graph/frame.ext.js');
	}
	
	public function srshtml() {
		$view  = C\view::inst('html');
		Cadmin::chkaccess($view);
		$where = array('status'=>1, 'stype'=>0);
		$rtmp  = C\model::load('pro.inc.rtmp')->get_column(0, -1, $where, 'rtmp');
		$html  = C\router::get_url('admin-live-log-graph', 'srshtml');
		$url   = C\router::get_url('admin-live-log-graph', 'srsjson');
		$view->assign_by_ref('html', $html);
		$view->assign_by_ref('url', $url);
		$view->assign_by_ref('rtmp', $rtmp);
		$view->display('live/log/graph/srs-graph.html');
	}
	
	public function srsjson() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ip    = input::get_post('ip');
		$where = '';
		$start = input::get_post('start');
		$end   = input::get_post('end');
		if (!empty($start) && !empty($start)) {
			$start = strtotime($start);
			$end   = strtotime($end);
		} else {
			$start = strtotime(date('Y-m-d', C\config::$nowtime));
			$end   = $start + 86400;
		}
		$orm   = C\model::load('log.log.netlog')->get_orm();
		$table = C\model::load('log.log.netlog')->get_table();
		if (!empty($ip)) {
			$sql = 'select max(nums) as maxnums, avg(nums) as avgnums, FROM_UNIXTIME(stime - stime % (10*60), "%Y-%m-%d %H:%i:%s") as stime
		from '.$table.' where ip like "%'.$ip.'%" and stime >= '.$start.' and stime <= '.$end.'
		group by stime - stime % (10*60)';
		} else {
			$sql = 'select sum(t.maxnums) as maxnums, sum(t.avgnums) as avgnums, t.stime from
		(select max(nums) as maxnums, avg(nums) as avgnums, FROM_UNIXTIME(stime - stime % (10*60), "%Y-%m-%d %H:%i:%s") as stime,ip from '.$table.' where stime >= '.$start.' and stime <= '.$end.' group by stime - stime % (10*60),ip '.') t
		group by stime';
		}
		$list  = $orm->get_list($sql);
		if (!empty($list)) {
			$avgnums = $maxnums = $time = array();
			foreach ($list as $item) {
				if (!$item['avgnums']) {
					continue;
				}
				$avgnums[] = round($item['avgnums'] ,2);
				$maxnums[] = $item['maxnums'];
				$time[]    = $item['stime'];
			}
			$res  = array('time'=>$time, 'avgnums'=>$avgnums, 'maxnums'=>$maxnums);
			ob_end_clean();
			echo json_encode($res);
		}
	}

	public function studio() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$graph = C\router::get_url('admin-live-log-graph', 'studiohtml');
		$view->assign_by_ref('graph', $graph);
		$view->display('live/log/graph/frame.ext.js');
	}
	
	public function studiohtml() {
		$view  = C\view::inst('html');
		Cadmin::chkaccess($view);
		$url   = C\router::get_url('admin-live-log-graph', 'studiojson');
		$view->assign_by_ref('url', $url);
		$view->display('live/log/graph/studio-graph.html');
	}
	
	public function studiojson() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$start  = input::get_post('start');
		if (empty($start)) {
			$start = date('Y-m-d', C\config::$nowtime);
		}
		$end  = input::get_post('end');
		if (empty($end)) {
			$end = date('Y-m-d', C\config::$nowtime);
		}
		if ($start == $end) {
			$end = $start + 24*3600;
		}
	    $start = strtotime($start);
		$end   = strtotime($end);
		$orm   = C\model::load('pro.sti.online')->get_orm();
		$table = C\model::load('pro.sti.online')->get_table();
		$sql    = 'select * from '.$table.' where stime >= '.$start . ' and stime <= '.$end;
		$list   = $orm->get_list($sql, 0, -1);
		if (!empty($list)) {
			$nums = $time = array();
			foreach ($list as $item) {
				$nums[] = $item['nums'];
				$time[] = date('Y-m-d H:i:s', $item['stime']);
			}
			$res  = array('nums'=>$nums, 'time'=>$time);
			ob_end_clean();
			echo json_encode($res);
		}
	}

	public function dolist() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);	
		$record = array(
			'total'=>3,
			'list'=>array(
				array('name'=>'帧率曲线图', 'url'=>C\router::get_url('admin-live-log-graph', 'frame', array('x'=>1))),
				array('name'=>'srs连接数', 'url'=>C\router::get_url('admin-live-log-graph', 'srs', array('x'=>2))),
				array('name'=>'直播间在线曲线图', 'url'=>C\router::get_url('admin-live-log-graph', 'studio', array('x'=>3))),
			)
		);
		$view->assign_by_ref('total', $record['total']);
		$view->assign_by_ref('dataset', $record['list']);
		$view->display();
	}
}